//usePage.js
import {ref, onMounted} from "vue";

/**
 * @param apiFuncs
 * @param queryForm
 */
export default function (apiFuncs, queryForm) {
    if (!apiFuncs) {
        apiFuncs = {
            pageFun: () => Promise.resolve(),
            deleteFun: () => Promise.resolve(),
        }
    }
    const {pageFun, deleteFun} = apiFuncs
    // 下面每个都用ref，也可以用一个reactive对象，然后toRefs对象
    const dataList = ref([]),
        pageIndex = ref(1),
        pageSize = ref(10),
        totalPage = ref(0),
        addOrUpdateVisible = ref(false),
        dataListLoading = ref(false),
        confirmTitle = ref(''),
        dataListSelections = ref([])

    function doGetDataList() {
        if (!(pageFun instanceof Function)) {
            // throw new Error("page参数为promise请求函数")
            return;
        }
        dataListLoading.value = true;
        let params = {
            pageno: pageIndex.value,
            pagesize: pageSize.value
        }
        if (queryForm) {
            let keys = Object.keys(queryForm);
            for (let i = 0, len = keys.length; i < len; i++) {
                let key = keys[i],
                    value = queryForm[key]
                if (value && value.trim() !== '') {
                    params[key] = value;
                }
            }
        }
        pageFun(params)
            .then((data) => { //获取返回数据
                if (data.code === 200) {
                    dataList.value = data.data.records;
                    totalPage.value = data.data.total
                }

                dataListLoading.value = false;
                return Promise.resolve(data)
            })
            .catch((e) => {
                // eslint-disable-next-line
                console.error(e)
                dataListLoading.value = false;
                return Promise.reject(e)
            });
    }

    function deleteConfirm(id) {
        let ids = id ? [id] : dataListSelections.value.map(item => {
            return item.id
        })
        confirmTitle.value = `提示：确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`;
        addDeleteId(ids)
    }

    function addDeleteId(ids) {
        dataListSelections.value = ids;
    }

    // 删除
    function doDeleteHandle() {
        const ids = dataListSelections.value;

        if (!ids || ids.length === 0) {
            return;
        }
        dataListSelections.value = []
         //只删除一个
        return deleteFun(ids[0]).then(() => {
            doGetDataList();
        })
    }

    // 每页数
    function sizeChangeHandle(val) {
        pageSize.value = val
        pageIndex.value = 1
        doGetDataList();
    }

    // 当前页
    function currentChangeHandle(val) {
        pageIndex.value = val
        doGetDataList();
    }

    // 多选
    function selectionChangeHandle(val) {
        dataListSelections.value = val
    }
    onMounted(() => {
        doGetDataList()
    })
    return {
        dataList,
        pageIndex,
        pageSize,
        totalPage,
        addOrUpdateVisible,
        dataListSelections,
        dataListLoading,
        doGetDataList,
        confirmTitle,
        deleteConfirm,
        doDeleteHandle,
        selectionChangeHandle,
        currentChangeHandle,
        sizeChangeHandle,
    }
}

